#include "camera.h"
/*
 *     //http://127.0.0.1:8080/?action=stream获取视频流的网络地址
 *  QUrl url("http://127.0.0.1:8080/?action=stream");
 *   Quest.setUrl(url);//设置请求的URL
 *   streamReply = Manager->get(Quest);//发送获取请求包
 *   //当服务器发送来数据时,就会发出readyRead信号,关联到onReadStream()
 *   connect(streamReply,SIGNAL(readyRead()),this,SLOT(onReadStream()));
*/
cameraclient::cameraclient(QObject *parent) : QObject(parent)
{
    connect(&tcpSocket,SIGNAL(readyRead()),this,SLOT(readImage()));
}

void cameraclient::connectToHost(const QHostAddress &ip, quint16 port){

    tcpSocket.connectToHost(ip,port);
}

void cameraclient::requestImage()
{
    int ret=0;
    QByteArray imareqArray;
    imareqArray.append("GET /?action=stream HTTP/1.1\r\n\r\n");
    ret=tcpSocket.write(imareqArray);
    if(ret==-1){
        qDebug("tcpSocket.write wrong!\n");
    }
}


void cameraclient::readImage(void)
{
    char spos[3]={(unsigned char)0xff,(unsigned char)0xd8};
    char epos[3]={(unsigned char)0xff,(unsigned char)0xd9};
    //qDebug("get data");
    imageArray.append(tcpSocket.readAll());
    int start=imageArray.indexOf(spos,0);
    if(start==-1){
        imageArray.clear();
        return;
    }
    //qDebug("Xget data");
    int end=imageArray.indexOf(epos,start);
    if(end==-1){
        //imageArray.clear();
        return;
    }
    //qDebug("get data");
    QByteArray bufArray=imageArray.mid(start,end-start+2);
    image.loadFromData(bufArray,"JPEG");
    emit newImageReady(image);
    //emit face(image);
    imageArray.clear();

}




cameraclient::~cameraclient()
{

}

